package com.h2psoft.truyen.tranh.activities;

import java.util.ArrayList;

import vn.persoft.extra.customview.CustomToast;
import vn.persoft.extra.customview.SlidingMenu.OnClosedListener;
import vn.persoft.extra.customview.SlidingMenu.OnOpenedListener;
import vn.persoft.extra.internet.Internet;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.ToggleButton;

import com.h2psoft.truyen.tranh.R;
import com.h2psoft.truyen.tranh.adapter.AdapterFragment;
import com.h2psoft.truyen.tranh.adapter.AdapterListComic;
import com.h2psoft.truyen.tranh.objects.ObjComic;
import com.h2psoft.truyen.tranh.server.ServerHandling;
import com.h2psoft.truyen.tranh.slidingmenu.BaseActivity;

public class FragmentListComic extends Fragment implements OnClickListener {
	private String tag = FragmentListComic.class.getName();

	private ListView lvlComic;
	private AdapterListComic adapterListComic;
	private ArrayList<ObjComic> arrComics;
	private boolean isNull;
	public static ToggleButton btnNew, btnMenu;

	public static Fragment newInstance(String title) {
		FragmentListComic mFragment = new FragmentListComic();

		Bundle args = new Bundle();
		args.putString("title", title);
		mFragment.setArguments(args);
		return mFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_list_comic, container, false);
		initView(v);
		return v;
	}

	private void initView(View v) {
		lvlComic = (ListView) v.findViewById(R.id.lvlComic);
		btnMenu = (ToggleButton) v.findViewById(R.id.imgMenu_ListComic);
		btnNew = (ToggleButton) v.findViewById(R.id.imgNewComic_ListComic);
		arrComics = new ArrayList<ObjComic>();
		adapterListComic = new AdapterListComic(getActivity(), arrComics);
		lvlComic.setAdapter(adapterListComic);
		btnMenu.setOnClickListener(this);
		btnNew.setOnClickListener(this);
		initFeature();
		initMenu();
	}

	// Copyright (C) 2011 - 2012 by PERSO STOCK COMPANY.
	private void initFeature() {
		lvlComic.setVisibility(View.GONE);
		if (!MainActivity.loadingDialog.isShowing())
			MainActivity.loadingDialog.show();
		if (Internet.isConnectedInternet(getActivity().getApplicationContext()))
			new Thread(new Runnable() {
				public void run() {
					synchronized (this) {
						try {
							ServerHandling getListComic = new ServerHandling();
							isNull = getListComic.getListComic(arrComics);
						} catch (Exception e) {
							Log.e(tag, e.toString());
						}
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								MainActivity.loadingDialog.dismiss();
								if (!isNull) {
									adapterListComic.notifyDataSetChanged();
									lvlComic.requestLayout();
									lvlComic.setVisibility(View.VISIBLE);
								}
							}
						});
					}
				}
			}).start();
		else {
			MainActivity.loadingDialog.dismiss();
			lvlComic.setVisibility(View.VISIBLE);
			CustomToast.showRandom(getActivity(), R.string.dont_connect_internet);
		}
	}

	private void initMenu() {
		BaseActivity.sm.setOnClosedListener(new OnClosedListener() {

			@Override
			public void onClosed() {
				btnMenu.setChecked(false);
			}
		});
		BaseActivity.sm.setOnOpenedListener(new OnOpenedListener() {

			@Override
			public void onOpened() {
				btnMenu.setChecked(true);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgMenu_ListComic:
			BaseActivity.sm.showMenu();
			break;

		case R.id.imgNewComic_ListComic:
			MainActivity.mPager.setCurrentItem(1, true);
			// (AdapterFragment) getFragmentManager().
			break;
		}
	}

}
